package com.thy.axq.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

/**
 * 功能：PropertiesUtil 读取propery属性 规则：propery 随时都有可能变更的方在这个里面
 */
public class PropertiesUtil {
	private static final String encoding = "UTF-8";
	private static Map<String, PropertiesConfiguration> propertiesMap = new HashMap<String, PropertiesConfiguration>();

	public static PropertiesConfiguration getConfig(String configName) {
		PropertiesConfiguration config = propertiesMap.get(configName);
		try {
			if (null == config) {
				config = new PropertiesConfiguration("./config/" + configName + ".properties");
				// config = new
				// PropertiesConfiguration("/home/smrz/activeMq.properties");
				config.setReloadingStrategy(new FileChangedReloadingStrategy());
				config.setEncoding(encoding);
				propertiesMap.put(configName, config);
			}
		} catch (ConfigurationException e) {
			System.out.println("----------------");
		}
		return config;
	}

	public static String getString(String configName, String key) {
		String str = "";
		try {
			str = getConfig(configName).getString(key);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return str;
	}

	public static int getInt(String configName, String key) throws Exception {
		String str = getConfig(configName).getString(key);
		try {
			return Integer.parseInt(str);
		} catch (Exception e) {
			throw new Exception("key = " + key + "取到的值不是整形！", e);
		}
	}

	public static Iterator<String> getKeys(String configName, String prefix) throws Exception {
		return getConfig(configName).getKeys(prefix);
	}

	public static String getAppPath(Class cls) {
		ClassLoader loader = cls.getClassLoader();
		String clsName = cls.getName() + ".class";
		Package pack = cls.getPackage();
		String path = "";
		if (pack != null) {
			String packName = pack.getName();
			clsName = clsName.substring(packName.length() + 1);
			if (packName.indexOf(".") < 0)
				path = packName + "/";
			else {
				int start = 0, end = 0;
				end = packName.indexOf(".");
				while (end != -1) {
					path = path + packName.substring(start, end) + "/";
					start = end + 1;
					end = packName.indexOf(".", start);
				}
				path = path + packName.substring(start) + "/";
			}
		}
		java.net.URL url = loader.getResource(path + clsName);
		String realPath = url.getPath();
		int pos = realPath.indexOf("file:");
		if (pos > -1)
			realPath = realPath.substring(pos + 5);
		pos = realPath.indexOf(path + clsName);
		realPath = realPath.substring(0, pos - 5);
		if (realPath.endsWith("!"))
			realPath = realPath.substring(0, realPath.lastIndexOf("/"));
		return realPath;
	}// getAppPath定义 结束

	public static void main(String[] args) {
		System.out.println(getAppPath(PropertiesUtil.class));
	}
}